package com.koron.css2.newProjectCenter.callback;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.clientCenter.Enum.ProcessStateEnum;
import com.koron.css2.common.utils.HttpUtils;
import com.koron.css2.newProjectCenter.bean.PjDelay;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjDelayQuery;
import com.koron.css2.newProjectCenter.bean.query.PjInfoQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjDelayMapper;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PjDelayCallback implements ProcessInterface {
    @Override
    public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
        try {
            if(operation == OPERATION_SUSPEND){ // 假如是废弃
                PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
                PjDelayMapper pjDelayMapper = factory.getMapper(PjDelayMapper.class);
                PjDelay pjDelay = pjDelayMapper.selectById(billId);
                PjInfoBean pjInfo = pjInfoMapper.selectByBillNo(pjDelay.getPjBillNo());
                pjInfo.setDelayFlag(0);
                pjInfo.setTerminationFlag(0);
                pjInfoMapper.update(pjInfo);
            }

            if (operation == 32 && fromNodeCode.equals(ProcessStateEnum.PROCESSING.getState())) {
                PjDelayMapper pjDelayMapper = factory.getMapper(PjDelayMapper.class);
                PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
                Map<String, String> mapDelay = new HashMap<>();
                PjDelayQuery pjDelayQuery = new PjDelayQuery();
                pjDelayQuery.setId(billId);
                PjDelay pjDelay = pjDelayMapper.getSingle(pjDelayQuery);
                String pjBillNo = pjDelay.getPjBillNo();
                PjInfoQueryBean pjInfoQueryBean = new PjInfoQueryBean();
                pjInfoQueryBean.setBillNo(pjBillNo);
                PjInfoBean pjInfoBean = pjInfoMapper.getSingle(pjInfoQueryBean);
                mapDelay.put("nodeCode", pjInfoBean.getProcessState());
                mapDelay.put("processInstanceId", pjInfoBean.getProcessInstanceId());
                mapDelay.put("overdueTime", pjDelay.getOverdueTime());
                String url = ApplicationConfig.workflowUrl + "/process/overdue/delay.htm";
                String data = HttpUtils.sendGetForm(url, mapDelay);
                MessageBean<?> workflowResult = JsonUtils.jsonToPojo(data, MessageBean.class);
                if (workflowResult.getCode() != Constant.MESSAGE_INT_SUCCESS) {
                    MessageBean<String> messageBean = MessageBean.create(Constant.MESSAGE_INT_FAIL, "调用工作流延期失败", String.class);
                    messageBean.setData(workflowResult.getDescription());
                    return messageBean;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date oldDelayTime = sdf.parse(pjDelay.getOldDelayTime());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(oldDelayTime);
                calendar.add(Calendar.SECOND, Integer.parseInt(pjDelay.getOverdueTime()));
                pjDelay.setNewDelayTime(sdf.format(calendar.getTime()));
                pjDelayMapper.update(pjDelay);
                pjInfoBean.setDelayFlag(0);
                pjInfoBean.setDelayState(1);
                pjInfoMapper.update(pjInfoBean);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程延期工作流回调成功", null);
        } catch (Exception e) {
            logger.error("工程延期工作流回调失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程延期工作流回调失败", null);
        }
    }

    @Override
    public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
        return null;
    }
}
